# Maintainer: Maciej Suminski <maciej.suminski@cern.ch>
#
# Based on Arch ngspice PKGBUILD:
# https://git.archlinux.org/svntogit/community.git/tree/trunk/PKGBUILD?h=packages/ngspice
#   Contributor: Kyle Keen <keenerd@gmail.com>
#   Contributor: Abhishek Dasgupta <abhidg@gmail.com>
#   Contributor: Jason Taylor <jftaylor21@gmail.com>
#   Contributor: Luis Henrique <lmello.009@gmail.com>
#   Contributor: Wayne Stambaugh <stambaughw@gmail.com>

_realname=ngspice
pkgbase=mingw-w64-${_realname}-git
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}-git"
pkgver=r4961.9395551
pkgrel=1
pkgdesc='Mixed-level/Mixed-signal circuit simulator based on Spice3f5, Cider1b1, and Xspice. (mingw-w64)'
url='https://ngspice.sourceforge.io/'
license=('BSD')
arch=('any')
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "git")
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
provides=("${MINGW_PACKAGE_PREFIX}-${_realname}")
conflicts=("${MINGW_PACKAGE_PREFIX}-ngspice")
source=("${_realname}"::"git+https://git.code.sf.net/p/ngspice/ngspice"
        "manual.pdf"::"https://ngspice.sourceforge.io/docs/ngspice-manual.pdf")
sha256sums=('SKIP'
            'SKIP')

pkgver() {
  cd "${srcdir}/${_realname}"
  printf "r%s.%s" "$(git rev-list --count HEAD)" "$(git rev-parse --short HEAD)"
}

prepare() {
  mv ./manual.pdf "${srcdir}/${_realname}/doc/"
  cd "${srcdir}/${_realname}"
  # This is a git repo clone so generate the build config.
  ./autogen.sh
}

build() {
  cd "${srcdir}/${_realname}"
  [[ -d "${srcdir}/build-shared-${CARCH}" ]] && rm -rf "${srcdir}/build-shared-${CARCH}"
  mkdir -p "${srcdir}/build-shared-${CARCH}" && cd "${srcdir}/build-shared-${CARCH}"

  # FS#45230, create so lib
  # shared lib sets flags and modifies headers, needs dedicated pass
  # adding --with-readline disables libngspice-0.dll
  ../${_realname}/configure \
              --prefix=${MINGW_PREFIX} \
              --build=${MINGW_CHOST} \
              --host=${MINGW_CHOST} \
              --enable-openmp \
              --enable-xspice \
              --enable-cider \
              --with-ngshared
  make

  cd "${srcdir}/${_realname}"
  [[ -d "${srcdir}/build-static-${CARCH}" ]] && rm -rf "${srcdir}/build-static-${CARCH}"
  mkdir -p "${srcdir}/build-static-${CARCH}" && cd "${srcdir}/build-static-${CARCH}"

  ../${_realname}/configure \
              --prefix=${MINGW_PREFIX} \
              --build=${MINGW_CHOST} \
              --host=${MINGW_CHOST} \
              --enable-xspice \
              --enable-cider \
              --enable-openmp
  make
}

package() {
  cd "${srcdir}/build-static-${CARCH}"
  make install DESTDIR="${pkgdir}"
  install -D -m644 "${srcdir}/${_realname}/COPYING" \
          "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
  install -D -m644  "${srcdir}/${_realname}/doc/manual.pdf" \
          "${pkgdir}${MINGW_PREFIX}/share/doc/${_realname}/manual.pdf"

  # library files have to be copied manually
  cd "${srcdir}/build-shared-${CARCH}"
  install -D -m755 src/.libs/libngspice-0.dll "${pkgdir}${MINGW_PREFIX}/bin"
  install -D -m755 src/.libs/libngspice.dll.a "${pkgdir}${MINGW_PREFIX}/lib"
}
